<template>
  <div>
    <el-dialog :title="'选择'+pType" :visible.sync="dialogTableVisible">
      <div class="departSearch">
        <el-input v-model="input" placeholder="请输入内容" class="pop_input" />
        <el-button type="success" class="pop_btn" @click="searchFun">搜索</el-button>
        <el-button type="primary" class="pop_btn" @click="sureFun">确认</el-button>
      </div>
      <Graytable ref="graytable" :t-head="tHead" :t-data="tData" @selectionChange="selectionChange" />
      <pagination :total="query.total" :page.sync="query.page" :limit.sync="query.limit" @pagination="getList" />
    </el-dialog>
  </div>
</template>

<script>
import { gslGetDeptListJson,
  gslGetDictDoctorListJson,
  gslGetDrugList,
  gslGetDictRouteListJson,
  gslGetDictDiseaseListJson,
  gslGetCurrentPatientOrdersList,
  gslGetCurrentPatientLabList, GetPatientDrugDisList, GetPatientDisList,
  GetCommentListJson } from '@/api/cpw'
export default {
  name: 'Poptable',
  props: {
    pType: {
      required: true,
      type: String
    },
    ctype: {
      type: String,
      default: 'mz'
    },
    setting: {
      type: Boolean,
      default: false
    },
    patientId: {
      type: String,
      default: ''
    },
    visitId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      query: {
        total: 0,
        page: 1,
        limit: 10
      },
      tHead: [],
      tData: [],
      popFlag: true,
      input: '',
      srm: '',
      type: '',
      selectLists: '',
      title: ''
    }
  },
  watch: {
    pType(e) {
      this.type = this.pType
      this.title = '选择' + this.type
      this.changeFun()
    },
    ctype() {
      this.changeFun()
    },
    patient_id() {
      console.log(this.patient_id)
      this.changeFun()
    },
    visit_id() {
      console.log(this.visit_id)
      this.changeFun()
    }
  },
  methods: {
    changeFun() {
      this.query.page = 1
      this.query.limit = 10
      this.srm = ''
      this.input = ''
      this.query.total = 0
      this.tData = []
      if (this.type === '科室') {
        this.tHead = [{ selection: true }, { prop: 'deptcode', label: '科室编码' }, { prop: 'deptname', label: '科室名称' }]
      } else if (this.type === '主管医生') {
        this.tHead = [{ selection: true }, { prop: 'doctorcode', label: '主管医生编码' }, { prop: 'doctorname', label: '主管医生姓名' }]
      } else if (this.type === '既往病史及药物治疗史') {
        this.tHead = [{ selection: true }, { prop: 'disname', label: '疾病名称' }, { prop: 'distime', label: '疾病时间' }, { prop: 'itemname', label: '药品名称' }, { prop: 'drugtype', label: '类型' }, { prop: 'drugform', label: '剂型' }, { prop: 'drugspec', label: '规格' }]
      } else if (this.type === '既往病史及非药物治疗史') {
        this.tHead = [{ selection: true }, { prop: 'disname', label: '疾病名称' }, { prop: 'distime', label: '疾病时间' }, { prop: 'treatdescription', label: '治疗描述' }, { prop: 'treattime', label: '治疗时间' }]
      } else if (this.type === '药疗医嘱' || this.type === '咨询对象') {
        this.tHead = [{ selection: true }, { prop: 'uniquecode', label: '药疗医嘱编码' }, { prop: 'drugname', label: '药疗医嘱名称' }]
      } else if (this.type === '给药途径' || this.type === '药品信息给药途径') {
        this.tHead = [{ selection: true }, { prop: 'routecode', label: '给药途径编码' }, { prop: 'routename', label: '给药途径名称' }]
      } else if (this.type === '诊断') {
        this.tHead = [{ selection: true }, { prop: 'discode', label: '诊断编码' }, { prop: 'disname', label: '诊断名称' }]
      } else if (this.type === '药品') {
        this.tHead = [{ selection: true }, { prop: 'uniquecode', label: '药品编码' }, { prop: 'drugname', label: '药品名称' }]
      } else if (this.type === '问题类型') {
        this.tHead = [{ selection: true }, { prop: 'commentsname', label: '问题类型' }]
      } else if (this.type === '导入医嘱对象' ||
        this.type === '导入医嘱要点' ||
        this.type === '导入医嘱' ||
        this.type === '用药建议医嘱对象' ||
        this.type === '用药建议医嘱要点' ||
        this.type === '简要记录-医嘱对象' ||
        this.type === '简要记录-医嘱要点') {
        this.tHead = [{ selection: true }, { prop: 'ordername', label: '药品名称' }, { prop: 'usagedosage', label: '用法用量' }, { prop: 'startdatetime', label: '开嘱时间' }]
      } else if (this.type === '导入检验' ||
        this.type === '用药建议导入检验' ||
        this.type === '简要记录-导入检验') {
        this.tHead = [{ selection: true }, { prop: 'labname', label: '检验名称' }, { prop: 'reporttime', label: '检验报告时间' }, { prop: 'labresult', label: '检验结果' }]
      }
      this.getList()
    },
    async getList() {
      let res
      if (this.type === '科室') {
        res = await gslGetDeptListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm })
        this.tData = res.rows
        this.query.total = res.records
      } else if (this.type === '主管医生') {
        res = await gslGetDictDoctorListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm })
        this.tData = res.Doctordata
        this.query.total = res.records
      } else if (this.type === '既往病史及药物治疗史') {
        res = await GetPatientDrugDisList({
          json: { Patient_id: this.patient_id, Visit_id: this.visit_id }})
        this.tData = res.PatientDrugDisListdata
        this.query.total = res.PatientDrugDisListdata.length
      } else if (this.type === '既往病史及非药物治疗史') {
        res = await GetPatientDisList({
          json: { Patient_id: this.patient_id, Visit_id: this.visit_id }})
        console.log('既往病史及非药物治疗史')
        console.log(res)
        this.tData = res.PatientDisListdata
        this.query.total = res.PatientDisListdata.length
      } else if (this.type === '药疗医嘱' || this.type === '药品' || this.type === '咨询对象') {
        res = await gslGetDrugList({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm })
        this.tData = res.rows
        this.query.total = res.records
      } else if (this.type === '给药途径' || this.type === '药品信息给药途径') {
        res = await gslGetDictRouteListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm })
        this.tData = res.Doctordata
        this.query.total = res.records
      } else if (this.type === '诊断') {
        res = await gslGetDictDiseaseListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm })
        this.tData = res.Dictdiseasedata
        this.query.total = res.records
      } else if (this.type === '问题类型') {
        res = await GetCommentListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm })
        this.tData = res.QuestionList
        this.query.total = res.records
      } else if (this.type === '导入医嘱对象' ||
        this.type === '导入医嘱要点' ||
        this.type === '导入医嘱' ||
        this.type === '用药建议医嘱对象' ||
        this.type === '用药建议医嘱要点' ||
        this.type === '简要记录-医嘱对象' ||
        this.type === '简要记录-医嘱要点') {
        res = await gslGetCurrentPatientOrdersList({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          json: { Patient_id: this.patient_id, Visit_id: this.visit_id }})
        console.log(this.query.limit)
        console.log(this.query.page)
        console.log(this.patient_id)
        console.log(this.visit_id)
        console.log(res)
        this.tData = res.CurrentPatientOrdersdata
        this.query.total = res.CurrentPatientOrdersdata.length
      } else if (this.type === '导入检验' ||
        this.type === '用药建议导入检验' ||
        this.type === '简要记录-导入检验') {
        res = await gslGetCurrentPatientLabList({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          json: { Patient_id: this.patient_id, Visit_id: this.visit_id }})
        this.tData = res.CurrentPatientLabdata
        this.query.total = res.CurrentPatientLabdata.length
      }
    },
    show() {
      this.dialogTableVisible = true
    },
    hide() {
      this.dialogTableVisible = false
    },
    searchFun() {
      this.query = {
        page: 1,
        limit: 10,
        total: 0
      }
      this.srm = this.input
      this.getList()
    },
    selectionChange(data) {
      this.selectLists = data
    },
    clearSelect() {
      this.selectLists = []
      this.$refs.graytable.toggleSelection()
    },
    sureFun() {
      if (this.type === '科室') {
        this.$emit('handleDeptcode', this.selectLists)
      }
      if (this.type === '主管医生') {
        this.$emit('handleDoctorcode', this.selectLists)
      }
      if (this.type === '药疗医嘱') {
        this.$emit('handleUniquecode', this.selectLists)
      }
      if (this.type === '咨询对象') {
        this.$emit('handleConsultationObject', this.selectLists)
      }
      if (this.type === '药品') {
        this.$emit('handleRsikUniquecode', this.selectLists)
      }
      if (this.type === '给药途径') {
        this.$emit('handleRoutecode', this.selectLists)
      }
      if (this.type === '药品信息给药途径') {
        this.$emit('handleMedicationRoutecode', this.selectLists)
      }
      if (this.type === '诊断') {
        this.$emit('handleDictDisease', this.selectLists)
      }
      if (this.type === '问题类型') {
        this.$emit('handleCommentList', this.selectLists)
      }
      if (this.type === '导入医嘱对象') {
        this.$emit('handleCurrentPatientOrdersListObj', this.selectLists)
      }
      if (this.type === '导入医嘱') {
        this.$emit('handleCurrentPatientOrdersList', this.selectLists)
      }
      if (this.type === '导入医嘱要点') {
        this.$emit('handleCurrentPatientOrdersListPoint', this.selectLists)
      }
      if (this.type === '导入检验') {
        this.$emit('handleCurrentPatientLabdata', this.selectLists)
      }

      if (this.type === '用药建议医嘱对象') {
        this.$emit('handleSuggestionObject', this.selectLists)
      }
      if (this.type === '用药建议医嘱要点' || this.type === '用药建议导入检验') {
        this.$emit('handleSuggestionPoints', this.selectLists)
      }

      if (this.type === '简要记录-医嘱对象') {
        this.$emit('handleSummaryRecordObj', this.selectLists)
      }
      if (this.type === '简要记录-医嘱要点' || this.type === '简要记录-导入检验') {
        this.$emit('handleSummaryRecordPoints', this.selectLists)
      }

      if (this.type === '既往病史及药物治疗史') {
        this.$emit('handlePatientDrugDisListdata', this.selectLists)
      }
      if (this.type === '既往病史及非药物治疗史') {
        this.$emit('handlePatientDisListdata', this.selectLists)
      }

      this.hide()
    }
    // async blurChange(prop, row) {
    //   const loading = this.$loading({
    // 		  lock: true,
    // 		  text: '正在处理中，请稍后',
    // 		  spinner: 'el-icon-loading',
    // 		  background: 'rgba(0, 0, 0, 0.7)'
    //   })
    //   const res = await this.$ptc.setDeptoutEdit({
    //     deptcode: row.deptcode,
    //     type: this.ctype,
    //     ksdm_encode: row.ksdm_encode,
    //     out_time: row[prop],
    //     ksmc: row.ksmc
    //   })
    //   loading.close()
    //   this.$message({
    // 		  message: res.msg,
    // 		  type: res.code == 1 ? 'success' : 'error',
    // 		  duration: 1500
    //   })
    //   if (res.code == 1) {
    //     this.getList()
    //   }
    // }
  }
}
</script>
<style lang="scss" scoped="scoped">
  .departSearch{
    display: flex;
    .pop_input{
      width: 300px;
    }
    .pop_btn{
      width: 70px;
      height: 40px;
      margin-left: 10px;
    }
  }
</style>
